package Java7_Concurrency_Cookbook.thread_synchronized_base.read_write_lock;

/**
 * 某一次执行结果如下 <br/>
 * Thread-2 : Price 1 : 1.0 <br/>
 * Thread-1 : Price 1 : 1.0 <br/>
 * Thread-3 : Price 1 : 1.0 <br/>
 * Writer : Prices have been modify --> 20.0,31.0 <br/>
 * Thread-0 : Price 1 : 1.0 <br/>
 * Thread-0 : Price 2 : 31.0 <br/>
 * Thread-0 : Price 1 : 20.0 <br/>
 * Thread-0 : Price 2 : 31.0 <br/>
 * 为什么 modify 之后 Thread-0 还能读取到数值 1 呢 ? <br/>
 * 因为 Reader 中打印语句和获取操作不是原子性操作 <br/>
 *
 * 变成人员应该确保正确的使用这些锁, 使用它们的时候应该符合这些锁的设计初衷. <br/>
 * 当你获取Lock接口的读锁时, 不可以进行修改操作, 否则将引起数据不一致的错误.
 */
public class Main {
    public static void main(String[] args) {
        PricesInfo pricesInfo = new PricesInfo();

        Reader[] readers = new Reader[5];
        Thread[] threadsReader = new Thread[5];
        for (int i = 0; i < 5; i++) {
            readers[i] = new Reader(pricesInfo);
            threadsReader[i] = new Thread(readers[i]);
        }

        Writer writer = new Writer(pricesInfo);
        Thread threadWriter = new Thread(writer);

        for (int i = 0; i < 5; i++) {
            threadsReader[i].start();
        }
        threadWriter.start();
    }
}
